package com.aitaoxue.palx.base;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.VelocityTracker;

import com.aitaoxue.palx.R;


public class AnimationSurfaceView extends SurfaceView implements Callback,
		Runnable {
	private Context context = null;

	// 控制surface
	private SurfaceHolder holder;

	private Paint paint;
	private Thread thread;
	// 控制线程的标识符
	private boolean flag;
	// 画布
	private Canvas canvas;
	public static int screenW, screenH;

	int currentViewNo = 0;
	private int oneTopCtrl = -1;
	float oneEarthRatOut = 0;
	int sign = -1;

	private Bitmap bmpEarth = null;
	private int bmpEarthX, bmpEarthY;
	private int ratX, ratY;

	public AnimationSurfaceView(Context context) {
		super(context);
		this.context = context;

		holder = this.getHolder();
		holder.addCallback(this);
		canvas = new Canvas();
		paint = new Paint();

		paint.setColor(Color.WHITE);
		paint.setAntiAlias(true);
		this.setFocusable(true);
	}

	@Override
	public void run() {
		while (flag) {
			long start = System.currentTimeMillis();
			myDraw();
			logic();
			long end = System.currentTimeMillis();
			try {
				if (end - start < 50) {
					Thread.sleep(50 - (end - start));
				}
			} catch (Exception e) {
			}
		}
	}

	public void myDraw() {
		canvas = holder.lockCanvas();
		if (canvas != null) {
			canvas.drawColor(Color.WHITE);
			canvas.save();
			canvas.rotate(oneEarthRatOut, ratX, ratY);
			canvas.drawBitmap(bmpEarth, bmpEarthX, bmpEarthY, paint);
			canvas.restore();
		}

		holder.unlockCanvasAndPost(canvas);
	}

	public void logic() {
		if(oneTopCtrl>=0&&oneTopCtrl<30){
			oneTopCtrl++;
			oneEarthRatOut+=3*sign;
			if(oneEarthRatOut>360){
				oneEarthRatOut%=360;
			}
		}
	}

	private VelocityTracker tracker = null;

	int detaX = 0;
	int tmpX = 0, tmpY = 0;

	int startX = 0, startY = 0;
	int endX = 0, endY = 0;

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		synchronized (event) {
			int action = event.getAction();
			switch (action) {
			case MotionEvent.ACTION_DOWN:
				startX = (int) event.getX();
				startY = (int) event.getY();
				break;
			case MotionEvent.ACTION_MOVE:
				tmpX = (int) event.getX();
				tmpY = (int) event.getY();
				break;
			case MotionEvent.ACTION_UP:
				endX = (int) event.getX();
				endY = (int) event.getY();

				int tmp = endX - startX;

				int dir = judgeDir(startX, startY, endX, endY);
				
				if(dir==1){
					if(currentViewNo>0){
						sign=1;
						oneTopCtrl=0;
						currentViewNo--;
					}
				}else if(dir==2){
					if(currentViewNo<3){
						sign=-1;
						oneTopCtrl=0;
						currentViewNo++;
					}
				}
				break;
			case MotionEvent.ACTION_CANCEL:
			case MotionEvent.ACTION_OUTSIDE:
				tracker.clear();
				tracker.recycle();
				break;
			default:
				break;
			}
		}
		return true;
	}
	
	private int judgeDir(int startX,int startY,int endX,int endY){
		if ((endX - startX) > 0 && Math.abs(endX - startX) > screenW / 3) {  
            // 方向是正右方 1  
            return 1;  
        } else if ((endX - startX) < 0 && Math.abs(endX - startX) > screenW / 3) {  
            // 方向是正左方 2  
            return 2;  
        }  
        return 0;
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		screenH = this.getHeight();
		screenW = this.getWidth();
		flag = true;

		initData();

		thread = new Thread(this);
		thread.start();
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		flag = false;
	}

	private void initData() {
		bmpEarth = BitmapFactory.decodeResource(getResources(),
				R.drawable.revolve_ground);

		initAllXY();
	}

	private void initAllXY() {
		bmpEarthX = screenW / 2 - bmpEarth.getWidth() / 2;
		bmpEarthY = screenH - bmpEarth.getHeight() / 4;

		ratX = screenW / 2;
		ratY = screenH + bmpEarth.getHeight() / 4;
	}

}
